﻿using System;
using System.Collections.Generic;
using System.Data;
using Litografia.BusinessEntities;
using Nortec.Security;

namespace Litografia.DataObjects
{
    public class DOEntity : DOBase
    {
        #region Declarations

        private readonly string getListSP;
        private readonly EntityType entity;

        #endregion

        #region Constructors

        public DOEntity(EntityType entity)
        {
            this.entity = entity;
            getListSP = entity + "GetList";
        }

        #endregion

        #region Methods: Public

        public List<BEEntity> GetList(string filter, string id)
        {
            var result = new List<BEEntity>();

            var command = DB.GetStoredProcCommand(getListSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@Id", DbType.String, String.IsNullOrEmpty(id) ? Convert.DBNull : id);
            DB.AddInParameter(command, "@Filtro", DbType.String, String.IsNullOrEmpty(filter) ? Convert.DBNull : filter);
            if (entity == EntityType.Producto || entity == EntityType.Material)
                DB.AddInParameter(command, "@SucursalId", DbType.Int32, NortecMembership.BranchId);

            var data = DB.ExecuteDataSet(command);
            if (data.Tables.Count == 1)
            {
                foreach (DataRow row in data.Tables[0].Rows)
                    result.Add(CreateEntityFromDataRow(row));
            }
            return result;
        }
        
        #endregion
    }
}
